/* eslint-disable eqeqeq */
import { createElement } from 'react'
import { Upload, message } from 'antd'
import { post, Update } from '@/services/https'
import { openNotificationWithIcon } from '@/utils/helper'
const h = createElement

const SUFFIX = /.+(.w+)$/,
  BYTE = 1024,
  ACCEPT = {
    zip:
      'application/zip,application/x-zip,application/x-zip-compressed,application/gzip,application/x-tar',
    pdf: 'application/pdf',
    excel:
      'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    image: 'image/jpeg,image/bmp,image/png,image/gif',
    txt: 'application/json, text/plain',
    stream: 'application/octet-stream',
  },
  getAccepts = (accept) =>
    (Array.isArray(accept) ? accept : [accept])
      .map((ac) => ACCEPT[ac])
      .join(','),
  beforeCheck = (config, file) => {
    let { accept, max = Number.MAX_VALUE } = config || {},
      { size, type } = file,
      accepts = getAccepts(accept).split(',')
    //大小限制(M)
    if (Math.pow(BYTE, 2) * max < size) {
      message.info(`文件不能超过${max}M`)
      return false
    }
  }

const UploadComponent = (props) => {
  let {
      children,
      config,
      group_id,
      parent_id,
      domain_id,
      temp_value,
      department_name,
      IPgroup_type,
      domain_name,
      selector_name,
      sts,
      fish,
      desc,
      backup_type,
    } = props,
    { accept } = config,
    attrs = {}
  //不能在props对象上直接添加属性，只能再定义一个attrs对象
  Object.assign(attrs, {
    action: '',
    accept: getAccepts(accept),
    beforeUpload: (file) => beforeCheck(config, file),
    customRequest: (opts) => {
      let { file, onSuccess, onProgress, onError } = opts,
        { uid, name, type } = file
      name = `${uid}${name.replace(SUFFIX, '$1')}`
      //判断上传的文件是否是图片，若不是图片，前端可自行根据isImg来控制是否可预览文件
      if (getAccepts(accept).indexOf(type) > -1) file.isImg = true
      const formData = new FormData()
      formData.append('file', file)
      if (sts) {
        if (
          domain_name == '' ||
          domain_name == undefined ||
          selector_name == '' ||
          selector_name == undefined
        ) {
          message.error('请填写生效域名或Selector名称')
          return
        } else {
          if (domain_name) {
            formData.append('domain_name', domain_name)
          }
          if (selector_name) {
            formData.append('selector_name', selector_name)
          }
        }
      }
      if (fish) {
        if (desc == '' || desc == undefined) {
          message.error('请填写描述')
          return
        } else {
          if (desc) {
            formData.append('desc', desc)
          }
        }
      }
      if (domain_id) {
        formData.append('domain_id', domain_id)
      }
      if (group_id) {
        formData.append('group_id', group_id)
      }
      if (parent_id) {
        formData.append('parent_id', parent_id)
      }
      if (department_name) {
        formData.append('department_name', department_name)
      }
      if (IPgroup_type) {
        formData.append('type', IPgroup_type)
      }
      if (temp_value) {
        formData.append('type', temp_value)
      }
      if (backup_type) {
        formData.append('backup_type', backup_type)
      }
      Update(config.url, formData).then((res) => {
        if (res.code == '-1') {
          openNotificationWithIcon('error', '上传', res.info)
        } else {
          openNotificationWithIcon('success', '上传', '上传成功')
          onSuccess(res)
        }
      })
    },
  })

  return h(Upload, { ...props, ...attrs }, children)
}

export default UploadComponent
